#
# @lc app=leetcode.cn id=110 lang=python3
#
# [110] 平衡二叉树
#

# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: TreeNode) -> bool:
        def maxDepth(root: TreeNode) -> int:
            def midOrder(treeNode):
                if treeNode == None:return
                dictA["depths"] += 1
                if treeNode.left != None:
                    midOrder(treeNode.left)       
                if treeNode.right != None:
                    midOrder(treeNode.right)
                dictA["maxDepth"] = max(dictA["maxDepth"], dictA["depths"])    
                dictA["depths"] -= 1
                return
            dictA = {"depths":0, "maxDepth":0}
            midOrder(root) 
            return dictA["maxDepth"]
        
        def isbala(treeNode):
            if treeNode == None:
                return True
            elif abs(maxDepth(treeNode.left)-maxDepth(treeNode.right)) > 1:
                return False
            else:
                return isbala(treeNode.left) and isbala(treeNode.right)
        
        return isbala(root)
               
    

   
# @lc code=end

